<?
    class Principio_Model_AccountMapper extends Principio_Library_Mapper
    {
        private static $db_table = null;

        private static function getDBTable()
        {
            if (self::$db_table == null)
                self::$db_table = new Principio_Model_DbTable_Account();

            return self::$db_table;
        }


        public static function deleteAll($company_id)
        {
            self::getDBTable()->delete(array("company_id=?" => $company_id));
        }



        public static function delete($id, $use_transaction = true)
        {
            try
            {
                if ($use_transaction)
                    self::getDBTable()->getAdapter()->beginTransaction();

                Principio_Model_AccountLinkMapper::reset($id);
                self::getDBTable()->delete(array("id=?" => $id));

                if ($use_transaction)
                    self::getDBTable()->getAdapter()->commit();

                return true;
            }
            catch (Exception $ex)
            {
                $return = self::reportException($ex);

                if ($use_transaction)
                {
                    self::getDBTable()->getAdapter()->rollback();
                    return false;
                }
                else
                    return $return;

            };
        }


        public static function checkUniqueLogin(Principio_Model_Account $model_account)
        {
            try
            {
                $select = self::getDBTable()->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART)->where("login=?", $model_account->getLogin())->where("company_id=?", $model_account->getCompanyID())->where("id<>?", $model_account->getID())->columns("id");
                return (is_null(self::getDBTable()->fetchRow($select)));
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function save(Principio_Model_Account &$model_account)
        {
            try
            {
                $data = array(
                                "company_id" => $model_account->getCompanyId(),
                                "login" => $model_account->getLogin(),
                                "password" => $model_account->getPassword(),
                                "short_name" => $model_account->getShortName(),
                                "display_name" => $model_account->getDisplayName()
                             );

                if ($model_account->getID() == -1)
                    return self::getDBTable()->insert($data);
                else
                    return self::getDBTable()->update($data, array("id=?" => $model_account->getID()));
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function get($id = null)
        {
            try
            {
                if (is_null($id))
                {
                    $accounts_raw = self::getDBTable()->fetchAll();
                    $accounts = array();
                    foreach ($accounts_raw as $account)
                        $accounts[$account->id] = new Principio_Model_Account($account->toArray());

                    return $accounts;
                }
                else
                {
                    if (intval($id) == -1)
                        return new Principio_Model_Account();
                    else
                    {
                        $account_raw = self::getDBTable()->find(intval($id));
                        return (is_null($account_raw) ? false : new Principio_Model_Account($account_raw[0]->toArray()));
                    };
                };
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function getInCompany($company_id)
        {
            try
            {
                $accounts_raw = self::getDBTable()->fetchAll(array("company_id=?" => $company_id));
                $accounts = array();
                foreach ($accounts_raw as $account)
                    $accounts[$account->id] = new Principio_Model_Account($account->toArray());

                return $accounts;
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }

        public static function search($string)
        {
            $search_fields = array(
                "login" => "Login",
                "display_name" => "Display name",
                "short_name" => "Short name"
            );

            $result = parent::searchInDB(self::getDBTable(), $search_fields, $string);

            foreach ($result as $res)
            {
                $model_search = new Principio_Model_Search($string, "Accounts");
                $model_search->setId($res["id"])->setResultName($res["login"])->setOpenItemFunctionName("Accounts")->setParentID($res["company_id"]);
                foreach ($res as $col_name => $value)
                {
                    if (strstr($value, $string) && in_array($col_name, array_keys($search_fields)))
                       $searchable_text[$search_fields[$col_name]] = $value;
                }
                $model_search->setResult($searchable_text);
                $search[$res["id"]] = $model_search;
            }
            return $search;
        }
    }
?>